package com.abbas.smartCity.ui.findJob;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.Death;
import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.BaseBean;
import com.abbas.smartCity.bean.findJob.ProfessionList;
import com.abbas.smartCity.bean.findJob.QueryResume;
import com.abbas.smartCity.retroft.ICallback;
import com.google.gson.Gson;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import retrofit2.Call;

public class AddResumeActivity extends BaseActivity {

    private CardView cardView;
    private LinearLayoutCompat qiuzhiInfo;
    private EditText workingExp;
    private EditText mostEdu;
    private EditText adress;
    private Spinner wantProf;
    private EditText wantMoney;
    private EditText mineDescription;
    private EditText EduHistory;
    private EditText workingHistory;
    private Button ok;
    private int type;
    private List<ProfessionList.RowsBean> professionListRows;
    private QueryResume.DataBean resume;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_resume;
    }

    @Override
    protected String setCustomTitle() {
        return "";
    }

    @Override
    protected void initView() {
        cardView = (CardView) findViewById(R.id.cardView);
        qiuzhiInfo = (LinearLayoutCompat) findViewById(R.id.qiuzhiInfo);
        workingExp = (EditText) findViewById(R.id.working_exp);
        mostEdu = (EditText) findViewById(R.id.most_edu);
        adress = (EditText) findViewById(R.id.adress);
        wantProf = (Spinner) findViewById(R.id.want_prof);
        wantMoney = (EditText) findViewById(R.id.want_money);
        mineDescription = (EditText) findViewById(R.id.mineDescription);
        EduHistory = (EditText) findViewById(R.id.EduHistory);
        workingHistory = (EditText) findViewById(R.id.workingHistory);
        ok = (Button) findViewById(R.id.ok);
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type", -1);

        professionListRows = Death.professionList.getRows();
        wantProf.setAdapter(new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_dropdown_item,
                professionListRows.stream().map(ProfessionList.RowsBean::getProfessionName).collect(Collectors.toList())
        ));

        switch (type){
            case -1:
                //新增
                Objects.requireNonNull(getSupportActionBar()).setTitle("新增简历");
                break;
            case 0:
                //修改
                Objects.requireNonNull(getSupportActionBar()).setTitle("修改简历");

                final String data = getIntent().getStringExtra("data");
                resume = new Gson().fromJson(data, QueryResume.DataBean.class);
                workingExp.setText(resume.getExperience());
                mostEdu.setText(resume.getMostEducation());
                adress.setText(resume.getAddress());
                wantMoney.setText(resume.getMoney());
                mineDescription.setText(resume.getIndividualResume());
                EduHistory.setText(resume.getEducation());
                break;
            default:
                break;
        }
    }

    @Override
    protected void initEvent() {
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final QueryResume.DataBean dataBean = new QueryResume.DataBean();
                dataBean.setExperience(workingExp.getText().toString());
                dataBean.setMostEducation(mostEdu.getText().toString());
                dataBean.setAddress(adress.getText().toString());
                final String wantPositionId = wantProf.getSelectedItem().toString();
                final Optional<ProfessionList.RowsBean> first = professionListRows.stream()
                        .filter(data -> data.getProfessionName().equals(wantPositionId))
                        .findFirst();
                dataBean.setPositionId(first.get().getId());
                dataBean.setMoney(wantMoney.getText().toString());
                dataBean.setIndividualResume(mineDescription.getText().toString());
                dataBean.setEducation(EduHistory.getText().toString());
                dataBean.setPositionName(first.get().getProfessionName());
                switch (type){
                    case -1:
                        //新增
                        api.postJobResume(createRequestBody(dataBean)).enqueue(new ICallback<BaseBean>() {
                            @Override
                            public void onSuccess(Call<BaseBean> call, BaseBean data) {
                                showToast(data.getMsg());
                            }
                        });
                        break;
                    case 0:
                        //修改
                        dataBean.setId(resume.getId());
                        api.changeJobResume(createRequestBody(dataBean)).enqueue(new ICallback<BaseBean>() {
                            @Override
                            public void onSuccess(Call<BaseBean> call, BaseBean data) {
                                showToast(data.getMsg());
                            }
                        });
                        break;
                    default:
                        break;
                }
            }
        });
    }
}